package com.oildatacollection.server.handler;

import com.oildatacollection.datareceive.RecvQueue;
import com.oildatacollection.datasend.SendQueue;
import com.oildatacollection.message.RecvMessage;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.socket.SocketChannel;
import javafx.util.Pair;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@ChannelHandler.Sharable
public class ReceiveMessageHandler extends ChannelInboundHandlerAdapter {
    private final RecvQueue recvQueue;
    private final SendQueue sendQueue;

    public ReceiveMessageHandler(RecvQueue recvQueue, SendQueue sendQueue) {
        this.recvQueue = recvQueue;
        this.sendQueue = sendQueue;
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        if (!(msg instanceof RecvMessage)) {
            log.error("channelRead: RecvMessage接收错误!");
            return;
        }
        RecvMessage recvMessage = (RecvMessage) msg;
        int wellID = (int) recvMessage.getValue("wellID");
        sendQueue.addAddress(wellID, (SocketChannel) ctx.channel());
        recvQueue.sendToQueue(recvMessage);
    }
}
